//
//  PublishViewController.m
//  group14
//
//  Created by lanou3g on 15/11/20.
//  Copyright © 2015年 吕志伟. All rights reserved.
//

#import "PublishViewController.h"

#import "SendModel.h"

#import "TextView.h"

@interface PublishViewController ()

// 输入框

@property (nonatomic, weak) UITextView *contentTextView;

@property (nonatomic, strong) UIImageView *photoImageView;


@end

@implementation PublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self loadContentTextView];
    
    ///self.photoData = [NSData data];
   // NSLog(@"data ====%@", self.photoData);
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(returnButtonItem:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(sendButtonItem:)];
    
    self.photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,CGRectGetMaxY(self.contentTextView.frame) + 10, 100, 100)];
    
    self.photoImageView.image = [UIImage imageWithData:self.photoData];
    
    self.photoImageView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.photoImageView];
}

// 取消按钮
- (void)returnButtonItem:(UIBarButtonItem *)item
{
    [self.navigationController popViewControllerAnimated:YES];
}
// 发送按钮
- (void)sendButtonItem:(UIBarButtonItem *)item
{
    
    NSLog(@"发送朋友圈内容");
    
    AVUser *user = [AVUser currentUser];
    
    AVFile *file = [AVFile fileWithData:self.photoData];
    
    
    SendModel * model = [[SendModel alloc] init];
    
    model.userName = user.username;
    
    model.contentStr = self.contentTextView.text;
    
    if (self.photoData == nil) {
        
        [model saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        
    }else {
        [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
        if (succeeded){
            
            NSLog(@"成功");
        } else {
            NSLog(@"失败 ==== %@",error);
        }
        
        }];
        
        model.photoFile = file;
    }
    
    
    [model saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    
}

// 加载contentTextView
- (void)loadContentTextView
{
    TextView *textView = [[TextView alloc] init];
    
    textView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3 );
    
    textView.font = [UIFont systemFontOfSize:15];
    
    textView.placeholder = @"分享你的心情,焕发新的自我...";
    
    textView.placeholderColor = [UIColor grayColor];
    
    [self.view addSubview:textView];
    
    self.contentTextView = textView;
    
    //监听通知
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:textView];
    

}

- (void)textDidChange
{
    self.navigationItem.rightBarButtonItem.enabled = self.contentTextView.hasText;
    
}



// 回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
